#!/usr/bin/python3
# -*- coding: utf-8 -*-

from playwright.sync_api import Page


class Mouse:
    def __init__(self, page: Page):
        self.page = page
        self.mouse = self.page.mouse

    def drag_and_drop(self, source, target):
        """按住元素从页面的一个位置拖动到另外一个位置"""
        self.page.drag_and_drop(source, target)

    def drag_to(self, locator_ele, target_ele):
        """先定位元素，调用drag_to方法到目标元素"""
        source = self.page.locator(locator_ele)
        target = self.page.locator(target_ele)
        source.drag_to(target)

    def mouse_wheel(self, x=0, y=0):
        """delta_x 横向移动距离,delta_y 纵向移动距离"""
        self.mouse.wheel(x, y)

    def mouse_wheel_and_wait(self, x=0, y=0, wait=500):
        """一边滚动一边加载网页"""
        self.mouse_wheel(x, y)
        self.page.wait_for_timeout(wait)

    def click(self, x=None, y=None, delay=0, button="left", click_count=1):
        """点击"""
        self.mouse.click(x, y, delay=delay, button=button, click_count=click_count)

    def dblclick(self, x=None, y=None, **kwargs):
        """双击"""
        self.mouse.dblclick(x, y, **kwargs)

    def down(self, **kwargs):
        """按住鼠标"""
        self.mouse.down(**kwargs)

    def move(self, x=None, y=None, **kwargs):
        """移动"""
        self.mouse.move(x, y, **kwargs)

    def up(self, **kwargs):
        """释放鼠标"""
        self.mouse.up(**kwargs)
